﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
namespace zichan.Controllers
{
    public class DepartmentRolesController : BaseController
    {
        // GET: DepartmentRoles
        public ActionResult Index(int? page, int? pageSize, int? pageCount, int? selectedPage)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(items, "Value", "Text", ViewBag.pageIndex);

            return View(DepartmentRoleService.List(page.Value, pageSize.Value));
        }
        public ActionResult Auth(int departmentId, int? page, int? pageSize, int? pageCount, int? selectedPage)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(items, "Value", "Text", ViewBag.pageIndex);

            ViewBag.departmentId = departmentId;
            return View(DepartmentRoleService.DepartmentRoleList(departmentId, page.Value, pageSize.Value));
        }
        public ActionResult CreateAuth(int departmentId)
        {
            ViewBag.departmentId = departmentId;
            List<int> rids = DepartmentRoleService.DepartmentRoleList(departmentId);
            var list = RoleService.List().Where(o => !rids.Contains(o.Id)).Select(o => new { o.Id, o.Name }).ToList();
            ViewBag.Roles = new SelectList(list, "Id", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult CreateAuth(DepartmentRole model)
        {
            model.RoleName = RoleService.Get(model.RoleId).Name;
            string constr = ConfigurationManager.ConnectionStrings["ZichanDBContext"].ConnectionString;
            string err = DepartmentRoleService.Add(model, constr);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#授权成功";
            }
            else
            {
                TempData["alert"] = "error#授权失败";
            }
            return RedirectToAction("Index");
        }
        public ActionResult DeleteAuth(int departmentId, int roleId, int id)
        {
            string constr = ConfigurationManager.ConnectionStrings["ZichanDBContext"].ConnectionString;
            string err = DepartmentRoleService.Delete(departmentId, roleId, id, constr);
            if(string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#成功删除";
            }
            else
            {
                TempData["alert"] = "error#删除失败";
            }
            return RedirectToAction("Index");
        }

    }
}